﻿Imports TNCDAL
Imports System.Data.Linq
Imports System.Linq

Public Class CVessel
    Dim db As New TanNamChinhDataContext

    Public Sub New()

    End Sub

    Public Sub New(ByVal dataContext As TanNamChinhDataContext)
        db = dataContext
    End Sub

    'Public Function GetVesselByID(ByVal ID_Vessel As Integer) As tblVessel
    '    Dim vessel = (From p In db.tblVessels
    '                  Where p.ID_Vessel = ID_Vessel
    '                  Select p).Single
    '    Return vessel
    'End Function

    'Public Function SelectAllRecord(ByVal id_Line As Integer) As IQueryable(Of tblVessel)
    '    Dim vessel = From p In db.tblVessels
    '              Select p

    '    Return vessel
    'End Function

    Public Function SelectAllRecord() As IQueryable(Of tblVessel)
        Dim vessel = From p In db.tblVessels
                     Where p.IsActive = True
                  Select p Order By p.VesselName

        Return vessel
    End Function

    Public Function SelectAllRecordByName(ByVal Name As String) As IQueryable(Of tblVessel)
        Dim vessel = From p In db.tblVessels
                     Where p.IsActive = True And p.VesselName.ToLower.Contains(Name.ToLower)
                  Select p Order By p.VesselName

        Return vessel
    End Function

    Public Function SelectRecordById(ByVal id As Integer) As tblVessel
        Dim vessel = (From p In db.tblVessels
                 Where p.ID_Vessel = id
                 Select p).Single
        Return vessel
    End Function

#Region "Code HT"
    ''' <summary>
    ''' Lấy danh sách Vessel theo Name (Name là duy nhất)
    ''' Create date: 23-12-2013
    ''' </summary>
    ''' <param name="name"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectRecordByName(ByVal name As String) As tblVessel
        Dim vessel = (From p In SelectAllRecord()
                 Where p.VesselName = name
                 Select p).Single
        Return vessel
    End Function
#End Region

    Public Sub Insert(ByVal obj As tblVessel)
        db.tblVessels.InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub

    Public Sub Update(ByVal obj As tblVessel)
        Dim vessel = SelectRecordById(obj.ID_Vessel)
        vessel.VesselName = obj.VesselName
        db.SubmitChanges()
    End Sub

    Public Sub Delete(ByVal id As Integer)
        Dim vessel = SelectRecordById(id)
        vessel.IsActive = False
        db.SubmitChanges()
        'Dim vessel = SelectRecordById(id)
        'db.tblVessels.DeleteOnSubmit(vessel)
        'db.SubmitChanges()
    End Sub
End Class
